<?php include("config.php");

// Connecting to database
if(!($link_id = mysql_connect($dbhost, $userid, $userpassword))) {
	die("<p><b>Error connecting to the database server:</b> " . mysql_error() . "</p>\n");
}
mysql_select_db($dbname);

// Requesting URL data
$feed      = $_GET["feed"];
$category  = $_GET["cat"];
if(empty($category) or($category == "") or($category == "All")) {
	$category = "All Categories";
}
if($category == "All Categories") {
	$catsql = "";
}
else {
	$catsql = "WHERE category LIKE '$category' OR category LIKE '" . str_replace(" &amp; "," & ",$category) . "%'";
}
// Establishing the SQL Query
$feedtitle = $sitename . " - New Releases and Updates in " . htmlspecialchars($category);
$sql = "SELECT * FROM paddata " . $catsql . " ORDER BY pdate desc, status LIMIT 20";
if($feed == "popular") {
	$feedtitle = $sitename . " - Most Popular Software in " . htmlspecialchars($category);
	$sql = "SELECT * FROM paddata " . $catsql . " ORDER BY dlcount desc, pdate desc LIMIT 20";
}
if($feed == "rating") {
	$feedtitle = $sitename . " - Top Rated Software in " . htmlspecialchars($category);
	$sql = "SELECT * FROM paddata " . $catsql . " ORDER BY siterating desc, pdate desc LIMIT 20";
}

header("Content-Type: text/xml; charset=iso-8859-1");

echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>\n";
echo "<rss version=\"2.0\">\n";
echo "	<channel>\n";
echo "		<title>" . $feedtitle . "</title>\n";
echo "		<description>" . $feedtitle . "</description>\n";
echo "		<pubDate>" . date("r") . "</pubDate>\n";
echo "		<lastBuildDate>" . date("r") . "</lastBuildDate>\n";
echo "		<link>http://" . $_SERVER['HTTP_HOST'] . "</link>\n";
echo "		<language>en-us</language>\n";
echo "		<category>Software</category>\n";
echo "		<docs>http://blogs.law.harvard.edu/tech/rss</docs>\n";
echo "		<ttl>600</ttl>\n"; // Time to live (sec)
echo "		<copyright>Copyright 2003-2006 " . $sitename . "</copyright>\n";
echo "		<image>\n";
echo "			<url>http://" . $_SERVER['HTTP_HOST'] . "/img/" . $prefix . "button.gif</url>\n";
echo "			<title>" . $sitename . " - Freeware and Shareware Downloads</title>\n";
echo "			<link>http://" . $_SERVER['HTTP_HOST'] . "</link>\n";
echo "			<width>88</width>\n";
echo "			<height>31</height>\n";
echo "		</image>\n";

// Receiving the required information from paddata
$numrows = 0;
$result = mysql_query($sql, $link_id);
if($result) $numrows = mysql_num_rows($result);
for ($i=0; $i<$numrows; $i++) {
	mysql_data_seek($result, $i);
	$query = mysql_fetch_array($result);
	
	$progid      = $query["progid"];
	$title       = htmlspecialchars($query["title"]);
	$version     = $query["version"];
	$pdate       = $query["pdate"];
	$description = htmlspecialchars($query["description"]);
	
	echo "		<item>\n";
	echo "			<title>" . $title . " " . $version . "</title>\n";
	echo "			<link>http://" . $_SERVER['HTTP_HOST'] . "/index04.php?progid=" . $progid . "</link>\n";
	echo "			<description>" . $description . "</description>\n";
	echo "			<guid>http://" . $_SERVER['HTTP_HOST'] . "/index04.php?progid=" . $progid . "</guid>\n";
	echo "			<pubDate>".date("r")."\n</pubDate>\n";
	echo "		</item>\n";
}
echo "	</channel>\n";
echo "</rss>";

// Disconnecting from database
mysql_close($link_id);

?>